#include "StdAfx.h"
#include <string>
#include <Util/StringUtil.h>
#include <interface/data_op/table_wrapper.h>
#include "clan_op.h"

namespace aot{ namespace data{

clan_op_impl::clan_op_impl(void)
{
}

clan_op_impl::~clan_op_impl(void)
{
}

bool
clan_op_impl::insert(const iclan_info* p)
{
    if ( !g_db_user ) return false;

    std::string str_clan_id = p->clan_id->c_str(); 
    std::string str_clan_name = p->clan_name->c_str();
    std::string str_creater_id = p->creater_id->c_str();
    std::string str_type_name = p->type_name->c_str();
    std::string str_keyword = p->keyword->c_str();
    std::string str_broadcast = p->broadcast->c_str();
    std::string str_remark = p->remark->c_str();
    std::string str_note = p->note->c_str();
    std::string str_create_datetime = p->create_datetime->c_str();

    remove_sin_quot(str_clan_id);
    remove_sin_quot(str_clan_name);
    remove_sin_quot(str_creater_id);
    remove_sin_quot(str_type_name);
    remove_sin_quot(str_keyword);
    remove_sin_quot(str_broadcast);
    remove_sin_quot(str_remark);
    remove_sin_quot(str_note);
    remove_sin_quot(str_create_datetime);
     
    std::string sql_fmt = 
        "insert into clan \
        (clan_id, \
         clan_name, \
        creater_id, \
        type_name, \
        keyword, \
        broadcast, \
        remark,   \
        note, \
        create_datetime, \
        type, \
        picture, \
        province, \
        city, \
        auth, \
        attribute, \
        version,\
        notify_mode, \
        save_to_server) \
        values\
        ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s',\
        %d, %d, %d, %d, %d, %d, %d, %d, %d);";

    std::string sql_exe;
    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(),
        str_clan_id.c_str(),
        str_clan_name.c_str(),
        str_creater_id.c_str(),
        str_type_name.c_str(),
        str_keyword.c_str(),
        str_broadcast.c_str(),
        str_remark.c_str(),
        str_note.c_str(),
        str_create_datetime.c_str(),
        
        p->type,
        p->picture,
        p->province,
        p->city,
        p->auth,
        p->attribute,
        p->version,
        p->notify_mode,
        p->save_to_server
        );

    return g_db_user->exec(sql_exe.c_str());
}

bool
clan_op_impl::del(const char* clan_id)
{
    std::string sql_fmt = "delete from clan where clan_id='%s';";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), clan_id);
    return g_db_user->exec(sql_exe.c_str());
}

bool
clan_op_impl::del_all()
{
    std::string sql_exe = "delete from clan;";
    return g_db_user->exec(sql_exe.c_str());
}

bool
clan_op_impl::update(const iclan_info* p)
{
    if ( !g_db_user ) return false;

    std::string str_clan_id = p->clan_id->c_str(); 
    std::string str_clan_name = p->clan_name->c_str();
    std::string str_creater_id = p->creater_id->c_str();
    std::string str_type_name = p->type_name->c_str();
    std::string str_keyword = p->keyword->c_str();
    std::string str_broadcast = p->broadcast->c_str();
    std::string str_remark = p->remark->c_str();
    std::string str_note = p->note->c_str();
    std::string str_create_datetime = p->create_datetime->c_str();

    remove_sin_quot(str_clan_id);
    remove_sin_quot(str_clan_name);
    remove_sin_quot(str_creater_id);
    remove_sin_quot(str_type_name);
    remove_sin_quot(str_keyword);
    remove_sin_quot(str_broadcast);
    remove_sin_quot(str_remark);
    remove_sin_quot(str_note);
    remove_sin_quot(str_create_datetime);

    std::string sql_fmt = 
        "update clan set \
        clan_name='%s', \
        creater_id='%s', \
        type_name='%s', \
        keyword='%s', \
        broadcast='%s', \
        remark='%s',   \
        note='%s', \
        create_datetime='%s', \
        type=%d, \
        picture=%d, \
        province=%d, \
        city=%d, \
        auth=%d, \
        attribute=%d, \
        version=%d,\
        notify_mode=%d, \
        save_to_server=%d \
        where clan_id='%s';";

    std::string sql_exe;
    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(),
        
        str_clan_name.c_str(),
        str_creater_id.c_str(),
        str_type_name.c_str(),
        str_keyword.c_str(),
        str_broadcast.c_str(),
        str_remark.c_str(),
        str_note.c_str(),
        str_create_datetime.c_str(),

        p->type,
        p->picture,
        p->province,
        p->city,
        p->auth,
        p->attribute,
        p->version,
        p->notify_mode,
        p->save_to_server,
        str_clan_id.c_str()
        );

    return g_db_user->exec(sql_exe.c_str());
}

bool
clan_op_impl::read(const char* clan_id, isqlite3_table** out)
{
    std::string sql_fmt = "select \
                          clan_id, \
                          clan_name, \
                          creater_id, \
                          type_name, \
                          keyword, \
                          broadcast, \
                          remark,   \
                          note, \
                          create_datetime, \
                          type, \
                          picture, \
                          province, \
                          city, \
                          auth, \
                          attribute, \
                          version,\
                          notify_mode,save_to_server from clan \
                          where clan_id='%s';";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), clan_id);
    return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}

bool
clan_op_impl::read(const char* clan_id, iclan_info* out)
{
    aot::data::table_helper t_table;
    bool ret =false;
    if (this->read(clan_id, t_table))
    {
        if ( t_table.get_rows_count() > 0 )
        {
            t_table.set_row_index(0);

            out->clan_id->assign(t_table.get_field_value_str("clan_id").c_str());
            out->clan_name->assign(t_table.get_field_value_str("clan_name").c_str());
            out->creater_id->assign(t_table.get_field_value_str("creater_id").c_str());
            out->type_name->assign(t_table.get_field_value_str("type_name").c_str());
            out->keyword->assign(t_table.get_field_value_str("keyword").c_str());
            out->broadcast->assign(t_table.get_field_value_str("broadcast").c_str());
            out->remark->assign(t_table.get_field_value_str("remark").c_str());
            out->note->assign(t_table.get_field_value_str("note").c_str());
            out->create_datetime->assign(t_table.get_field_value_str("create_datetime").c_str());
            
            out->type = t_table.get_field_value_int("type");
            out->picture = t_table.get_field_value_int("picture");
            out->province = t_table.get_field_value_int("province");
            out->city = t_table.get_field_value_int("city");
            out->auth = t_table.get_field_value_int("auth");
            out->attribute = t_table.get_field_value_int("attribute");
            out->version = t_table.get_field_value_int("version");
            out->notify_mode = t_table.get_field_value_int("notify_mode");
            out->save_to_server = t_table.get_field_value_int("save_to_server");

            ret = true;
        }
    }
    return ret;
}

bool
clan_op_impl::read(const char* clan_id, iclan_info** out)
{
    aot::data::iclan_info* obj = new aot::data::clan_info_impl();
    *out = obj;
    return this->read(clan_id, obj);
}

bool
clan_op_impl::get_clans(isqlite3_table** out)
{
    std::string sql_exe = "select \
                          clan_id, \
                          clan_name, \
                          creater_id, \
                          type_name, \
                          keyword, \
                          broadcast, \
                          remark,   \
                          note, \
                          create_datetime, \
                          type, \
                          picture, \
                          province, \
                          city, \
                          auth, \
                          attribute, \
                          version,\
                          notify_mode,save_to_server from clan;";
   
    return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}

int
clan_op_impl::get_members_count(const char* clan_id)
{
    int total;
    std::string sql_fmt = "select count(*) as t from clan_member where clan_id='%s';";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), clan_id);
    total = g_db_user->exec_scalar(sql_exe.c_str());
    return total;
}

}} /* end namespace aot/data*/
